<script setup lang="ts">
import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import {
  genDefaultFieldsAddApi,
  genDefaultFieldsEditApi,
  genDefaultFieldsQueryByIdApi,
} from '#/api/gen/defaultFields';
import { genFieldMappingQueryListApi } from '#/api/gen/fieldMapping';
import { queryDictOptionsApi } from '#/utils';

const record = ref({});
const [Form, formApi] = useVbenForm({
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
  },
  schema: [
    {
      component: 'ApiRadioGroup',
      defaultValue: 0,
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        afterFetch: (data) => {
          return data.map((item: any) => ({
            label: item.itemText,
            value: Number.parseInt(item.itemVal),
          }));
        },
        api: queryDictOptionsApi,
        params: {
          dictCode: 'yes_or_no',
        },
      },
      fieldName: 'isCommon',
      label: '通用字段',
      rules: 'required',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        afterFetch: (data) => {
          return data.map((item: any) => ({
            label: item.itemText,
            value: item.itemVal,
          }));
        },
        api: queryDictOptionsApi,
        params: {
          dictCode: 'online_table_type',
        },
        placeholder: '请选择表类型',
        allowClear: true,
      },
      fieldName: 'tableType',
      rules: 'required',
      label: '表类型',
      dependencies: {
        show(value) {
          return value.isCommon !== 0;
        },
        triggerFields: ['isCommon'],
      },
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入字段名称',
      },
      fieldName: 'fieldName',
      label: '字段名称',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入字段注释',
      },
      fieldName: 'fieldAnnotation',
      label: '字段注释',
      rules: 'required',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        afterFetch: (data) => {
          return data.map((item: any) => ({
            value: item.fieldType,
            label: item.remark
              ? `${item.fieldType} (${item.remark})`
              : `${item.fieldType} (${item.javaFieldType})`,
          }));
        },
        api: genFieldMappingQueryListApi,
        placeholder: '请选择字段类型',
        allowClear: true,
      },
      fieldName: 'fieldType',
      label: '字段类型',
      rules: 'required',
    },
    {
      component: 'Input',
      defaultValue: 32,
      componentProps: {
        placeholder: '请输入字段长度',
      },
      fieldName: 'fieldLength',
      label: '字段长度',
      rules: 'required',
    },
    {
      component: 'Input',
      defaultValue: 0,
      componentProps: {
        placeholder: '请输入小数点',
      },
      fieldName: 'decimalPoint',
      label: '小数点',
      rules: 'required',
    },
    {
      component: 'ApiRadioGroup',
      defaultValue: 1,
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        afterFetch: (data) => {
          return data.map((item: any) => ({
            label: item.itemText,
            value: Number.parseInt(item.itemVal),
          }));
        },
        api: queryDictOptionsApi,
        params: {
          dictCode: 'yes_or_no',
        },
      },
      fieldName: 'isPrimaryKey',
      label: '主键',
      rules: 'required',
    },
    {
      component: 'ApiRadioGroup',
      defaultValue: 0,
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        afterFetch: (data) => {
          return data.map((item: any) => ({
            label: item.itemText,
            value: Number.parseInt(item.itemVal),
          }));
        },
        api: queryDictOptionsApi,
        params: {
          dictCode: 'yes_or_no',
        },
      },
      fieldName: 'isNull',
      label: '允许为空',
      rules: 'required',
    },
    {
      component: 'ApiRadioGroup',
      defaultValue: 0,
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        afterFetch: (data) => {
          return data.map((item: any) => ({
            label: item.itemText,
            value: Number.parseInt(item.itemVal),
          }));
        },
        api: queryDictOptionsApi,
        params: {
          dictCode: 'yes_or_no',
        },
      },
      fieldName: 'isHandle',
      label: '可操作',
      rules: 'required',
    },
    {
      component: 'ApiRadioGroup',
      defaultValue: 0,
      rules: 'required',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        afterFetch: (data) => {
          return data.map((item: any) => ({
            label: item.itemText,
            value: Number.parseInt(item.itemVal),
          }));
        },
        api: queryDictOptionsApi,
        params: {
          dictCode: 'status',
        },
      },
      fieldName: 'status',
      label: '状态',
    },
  ],
});

const [Modal, modalApi] = useVbenModal({
  title: '在线设计表默认字段',
  onCancel: () => {
    modalApi.close();
  },
  onConfirm: onSubmit,
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      record.value = modalApi.getData<Record<string, any>>();
      modalApi.setState({
        title: `${record.value.id ? '编辑' : '新增'}设计表默认字段`,
      });
      if (record.value.id) {
        initFun();
      }
    }
  },
});

async function initFun() {
  const res = await genDefaultFieldsQueryByIdApi({ id: record.value.id });
  await formApi.setValues(res, false);
}
async function onSubmit() {
  const { valid } = await formApi.validate();
  const values = await formApi.getValues();
  if (valid) {
    if (record.value.id) {
      const msg = await genDefaultFieldsEditApi(values);
      message.success(msg);
      await modalApi.close();
    } else {
      const msg = await genDefaultFieldsAddApi(values);
      message.success(msg);
      await modalApi.close();
    }
  }
}
</script>

<template>
  <Modal>
    <Form />
  </Modal>
</template>
